2024.6.21 四則演算の速度比較
code:operand1.py
import time
loopmax = 100000000
#
time1_init = time.process_time()
for i in range(loopmax):
y = 2.0 + 3.0
time1 = time.process_time() - time1_init
print('+ :', time1)
#
time2_init = time.process_time()
for i in range(loopmax):
y = 2.0 - 3.0
time2 = time.process_time() - time2_init
print('- :', time2)
#
time3_init = time.process_time()
for i in range(loopmax):
y = 2.0 * 3.0
time3 = time.process_time() - time3_init
print('* :', time3)
#
time4_init = time.process_time()
for i in range(loopmax):
y = 2.0 / 3.0
time4 = time.process_time() - time4_init
print('/ :', time4)
結果
code:result1.txt
+ : 1.6219694999999998
- : 1.6305571
* : 1.6221865999999996
/ : 1.6243963
四則演算の間には、計算に要する時間に有意な差はみられないようだ。